package com.lv.phuotbacnam.ui.widget.wizard;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewFlipper;

/**
 * Flipper wizzard
 */
public abstract class FlipperWizard<T> extends Wizard<T>{

    /**Data view holder*/
    private ViewFlipper viewFlipper;

    /**Current page*/
    private int currentPage;

    public FlipperWizard(ViewFlipper viewFlipper) {
        this.viewFlipper = viewFlipper;
    }

    public void addPage(View view) {
        viewFlipper.addView(view);
    }

    public void addPage(ViewGroup container, int position){
        addPage(createPage(container, position));
    }

    public void next(){
        jumpTo(currentPage + 1);
    }

    public void previous(){
        jumpTo(currentPage - 1);
    }

    @Override
    public int getCurrentPage() {
        return viewFlipper.getDisplayedChild();
    }

    public int getItemsCount(){
        return viewFlipper.getChildCount();
    }

    public void jumpTo(int page){
        OnNavigationChangedListener listener = getOnNavigationChangedListener();
        if(page == -1){
            if(listener != null){
                listener.onLowerBoundTouched();
            }
        } else if(page == getItemsCount()){
            if(listener != null){
                listener.onUpperBoundTouched();
            }
        } else {
            currentPage = page;
            viewFlipper.setDisplayedChild(currentPage);
            if(listener != null){
                listener.onPageSelected(currentPage);
            }
        }
    }

    public View createPage(ViewGroup container, int position){
        throw new IllegalArgumentException("This should be overridden");
    }

    public abstract T getData();

}
